package com.superm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.superm.entity.*;
import com.superm.service.RepostoryService;
import com.superm.service.ShelfApplicationService;
import com.superm.service.StockService;
import com.superm.service.StorgageRackService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 上架申请单控制器
 */
@RequestMapping("/shelfapplication")
@Controller
public class ShelfApplicationController {

    @Autowired
    private StockService stockService;

    @Autowired
    private StorgageRackService storgageRackService;

    @Autowired
    private ShelfApplicationService shelfApplicationService;

    @Autowired
    private RepostoryService repostoryService;


    /**
     * 跳转到申请单添加
     * @param goodsId
     * @return
     */
    @RequestMapping("/skipShelfApplication")
    public ModelAndView skipShelfApplication(@RequestParam("goodsId") Integer goodsId){
        //根据商品编号查询底下的对应库存信息
        Stock  stock =  stockService.selectGoodsId(goodsId);
        //查询所有货架
        //查询货架信息
        List<StorgageRack> storgageRacksList = storgageRackService.selectStorgageRack(null,null,null);
        ModelAndView mv = new ModelAndView("shelfApplication/addShelfApplication");
        mv.addObject("storgageRacksList", storgageRacksList);
        mv.addObject("stock", JSON.toJSONString(stock));
        mv.addObject("stock1",stock);
        return mv;
    }


    /**
     * 模糊查询上架申请单信息
     * @param shelfApplicationPoJo
     * @return
     */
    @RequestMapping("/likeShelfApplication")
    @ResponseBody
    public Map<String,Object> likeShelfApplication(ShelfApplicationPoJo shelfApplicationPoJo,@RequestParam("page")Integer pageNo,@RequestParam("limit")Integer pageSize){
        Map<String,Object> map = shelfApplicationService.likeShelfApplication(shelfApplicationPoJo,pageNo,pageSize);
        return map;
    }

    /**
     * 跳转至上架申请单页面
     * @return
     */
    @RequestMapping("/skipShelfApplicationList")
    public ModelAndView skipShelfApplication(){
        ModelAndView mv= new ModelAndView("shelfApplication/shelfApplicationList");
        //查询当前用户的部门
        //获取认证主体
        Subject subjet = SecurityUtils.getSubject();
        //从认证主体取出认证信息
        Emp emp = (Emp)subjet.getPrincipal();
        mv.addObject("emp",emp);
        return mv;
    }

    /**
     * 添加申请单信息
     * @param shelfApplicationPoJo
     * @return
     */
    @RequestMapping("/addShelfApplication")
    @ResponseBody
    public Map<String,Object> addShelfApplication(ShelfApplicationPoJo shelfApplicationPoJo, ShelfDetailsPoJo shelfDetailsPoJo, HttpSession httpSession){
        System.out.println(shelfApplicationPoJo);
        System.out.println(shelfDetailsPoJo);
        //把传递过来的订单和订单详情进行传递
        Map<String,Object> map = shelfApplicationService.addShelfApplication(shelfApplicationPoJo,shelfDetailsPoJo,httpSession);
        return  map;
    }


    /**
     * 多商品上架申请单添加
     * @param shelfApplicationPoJo
     * @param shelfApplicationDetails
     * @return
     */
    @RequestMapping("/addShelfApplications")
    @ResponseBody
    public Map<String,Object> addShelfApplications(ShelfApplicationPoJo shelfApplicationPoJo,@RequestParam("shelfApplicationDetails[]") List<String> shelfApplicationDetails){
      Map<String,Object> map =  shelfApplicationService.addShelfapplications(shelfApplicationPoJo,shelfApplicationDetails);
        return map;
    }

    /**
     * 跳转至多个商品添加页面
     * @return
     */
    @RequestMapping("/skipAddShelfApplications")
    public ModelAndView skipAddShelfApplications(){
        ModelAndView mv = new ModelAndView("shelfApplication/addShelfApplications");
        //查询所有仓库
        List<RepostoryType> repostoryTypes = repostoryService.allRepostory();
        //查询所有货架
        List<StorgageRack> storgageRacks  =  storgageRackService.selectStorgageRack(null,null,null);
        mv.addObject("repostoryTypes",repostoryTypes);
        mv.addObject("storgageRacks",storgageRacks);
        return mv;
    }



    /**
     * 跳转至商品补货页面
     * @return
     */
    @RequestMapping("/skipreplenishments")
    public ModelAndView skipskipreplenishments(){
        ModelAndView mv = new ModelAndView("shelfApplication/replenishments");
        //查询所有仓库
        List<RepostoryType> repostoryTypes = repostoryService.allRepostory();
        mv.addObject("repostoryTypes",repostoryTypes);
        return mv;
    }


    /**
     * 审批通过
     * @return
     */
    @RequestMapping("/approvalBy")
    @ResponseBody
    public Map<String,Object> approvalBy(@RequestParam("aspplicationCode") String aspplicationCode){
        Map<String,Object> map = shelfApplicationService.approvalBy(aspplicationCode);
        return map;
    }


    /**
     * 跳转到驳回原因页面
     * @return
     */
    @RequestMapping("/skipRejectCause")
    public ModelAndView skipRejectCause(@RequestParam("aspplicationCode") String aspplicationCode){
        ModelAndView mv = new ModelAndView("shelfApplication/rejectCause");
        //放入订单号
        mv.addObject("aspplicationCode",aspplicationCode);
        return mv;
    }

    /**
     * 申请驳回
     * @param shelfApplication
     * @return
     */
    @RequestMapping("/rejectApplyFor")
    @ResponseBody
    public Map<String,Object> rejectApplyFor(ShelfApplication shelfApplication){
        System.out.println(shelfApplication);
        Map<String,Object> map = shelfApplicationService.rejectApplyFor(shelfApplication);
        return map;
    }


    /**
     * 跳转到填写出库备注页面
     * @return
     */
    @RequestMapping("/skipOutboundGoods")
    public ModelAndView skipOutboundGoods(@RequestParam("sapplicationCode") String aspplicationCode){
        //编号获取填写一下备注
        ModelAndView mv = new ModelAndView("shelfApplication/OutboundGoods");
        mv.addObject("aspplicationCode",aspplicationCode);
        return  mv;
    }


    /**
     * 库存出库
     * @return
     */
    @RequestMapping("/inventory")
    @ResponseBody
    public Map<String,Object> inventory(@RequestParam("sapplicationCode") String aspplicationCode,@RequestParam("remarkes") String remarkes){
         Map<String,Object>  map =  shelfApplicationService.putInStorage(aspplicationCode,remarkes);
        return map;
    }


    /**
     * 商品上架
     * @return
     */
    @RequestMapping("/goodsShelves")
    @ResponseBody
    public Map<String,Object> goodsShelves(@RequestParam("sapplicationCode") String aspplicationCode){
        Map<String,Object> map  = shelfApplicationService.goodsShelves(aspplicationCode);
        return map;
    }

}
